/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.firebird.oms.web.dwr;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.firebird.oms.constants.AuthConstants;
import com.firebird.oms.domain.sm.TismMenu;
import com.firebird.oms.service.sm.AuthService;
import com.firebird.oms.tools.Md5PasswordEncoder;
import com.firebird.oms.user.UserDetails;
import com.firebird.oms.web.base.BaseDwr;
import com.firebird.oms.web.util.DwrAuthHelper;

/**
 * @author qiuxj
 * @date 2009-11-16
 */
public class AuthDwr extends BaseDwr implements Serializable {

	private static final long serialVersionUID = -5209613697486460336L;
	private AuthService authService;

	/**
	 * 用户登录方法
	 * 
	 * @param logincode
	 * @param password
	 * @return
	 */
	public Map<String, Object> login(String logincode, String password) {
		Map<String, Object> map = authService.doAuth(logincode, Md5PasswordEncoder.encodePassword(password, AuthConstants.MD5_SALT),
				super.getSessionId(), super.getIpAddress(), "");
		
		if (map.get(AuthConstants.LOGIN_ID) != null) {
			UserDetails ud = new UserDetails();
			ud.setLoginid((Integer)map.get(AuthConstants.LOGIN_ID));
			ud.setUsername(map.get(AuthConstants.LOGIN_USERNAME).toString());
			ud.setLogincode(logincode);
			ud.setLoginnum((Integer) map.get(AuthConstants.LOGIN_NUM));
			ud.setLoginlevel((Integer) map.get(AuthConstants.LOGIN_LEVEL));
			ud.setLastChgPwdTime((Date) map.get(AuthConstants.PASSWD_LAST_CHGDATE));
			ud.setStaffId((Integer)map.get(AuthConstants.STAFF_ID));
			ud.setUpStaffId((Integer)map.get(AuthConstants.UP_STAFF_ID));
			DwrAuthHelper.setUser(ud);

			map.clear();
			
			map.put("loginid", ud.getLoginid());
			map.put("logincode", ud.getLogincode());
			map.put("username", ud.getUsername());
			map.put("state", ud.getState());
			map.put("loginnum", ud.getLoginnum());
			map.put("loginlevel", ud.getLoginlevel());
			map.put("lastChgPwdTime", ud.getLastChgPwdTime());
			map.put("lastLoginTime", ud.getLastLoginTime());
			map.put("staffId", ud.getStaffId());
		}
		return map;
	}
	
	/**
	 * 用户注销方法
	 */
	public void logoff() {
		UserDetails ud = (UserDetails) DwrAuthHelper.getUser();
		DwrAuthHelper.removeUser();
		authService.doLogout(ud.getLoginid(), super.getSessionId(), super.getIpAddress(), "");
	}
	
	/**
	 * 修改密码
	 * @param password
	 */
	public void doChangePassword(String password) {
		UserDetails user = super.getCurrentUser();
		authService.doChangePassword(user.getLoginid(), Md5PasswordEncoder.encodePassword(password, AuthConstants.MD5_SALT));
	}
	
	/**
	 * 取得当前员工有效菜单方法,指定菜单号获取其子菜单
	 * 
	 * @param menuCode
	 * @return
	 */
	public List<TismMenu> getMainMenusOfWorker(int menuCode) {
		return authService.findMenus(super.getCurrentUser(), menuCode);
	}
	
	/**
	 * 判断用户输入的密码是否在最近5次的密码修改记录 如果是，则不能修改密码（返回false）
	 * @param loginId
	 * @param password
	 * @return
	 */
	public boolean isPwdChg(String password){
		UserDetails user = super.getCurrentUser();
		return authService.isPwdChg(user.getLoginid(), Md5PasswordEncoder.encodePassword(password, AuthConstants.MD5_SALT));
	}

	public void setAuthService(AuthService authService) {
		this.authService = authService;
	}
	
	public static void main(String[] args) {
		System.err.println(Md5PasswordEncoder.encodePassword("000000", AuthConstants.MD5_SALT));
	}
	
}
